package cnn.testmpt;

import org.junit.Test;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AppTest {

    @Test
    public void test() {
        Person person = new Person();
        getFieldInfo(person::orgId,"0001");
        getFieldInfo(person::opDate,new Date());
    }

    public static void getFieldInfo(MyFunction fun, Object value) {
        System.out.println("");
        System.out.println("传入的实现类：" + fun.getImplClass());
        System.out.println("传入的方法名：" + fun.getImplMethodName());
        System.out.println("传入的参数值：" + value);
//        System.out.println("传入数据类型：" + fun.get().getClass());
    }

    /**
     * 关键在于这个方法
     */
    private static Map<Class, SerializedLambda> CLASS_LAMDBA_CACHE = new ConcurrentHashMap<>();
    public static SerializedLambda getSerializedLambda(Serializable fn) {
        SerializedLambda lambda = CLASS_LAMDBA_CACHE.get(fn.getClass());
        if(lambda == null) {
            try {
                Method method = fn.getClass().getDeclaredMethod("writeReplace");
                method.setAccessible(Boolean.TRUE);
                lambda = (SerializedLambda) method.invoke(fn);
                CLASS_LAMDBA_CACHE.put(fn.getClass(), lambda);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return lambda;
    }


}



